from sqlalchemy.orm import Session
from models import Friend


def create_firend(db:Session, first_name, last_name, age):
    new_firend = Friend(first_name=first_name, last_name=last_name, age=age)
    db.add(new_firend)
    db.commit()
    db.refresh(new_firend)
    return new_firend


def get_firend(db:Session, id:int):
    db_friend = db.query(Friend).filter(Friend.id==id).first()
    return db_friend


def list_firend(db:Session):
    all_firend = db.query(Friend).all()
    return all_firend


def update_friend(db:Session, id:int, first_name:str, last_name:str, age:int):    
    db_firend = get_firend(db=db, id=id)
    db_firend.first_name = first_name
    db_firend.last_name = last_name
    db_firend.age = age
    db.commit()
    db.refresh(db_firend)
    return db_firend


def delete_firend(db:Session, id:int):
    db_firend = get_firend(db=db, id=id)
    db.delete(db_firend)
    db.commit()
    return db_firend


